package com.mediocre.mapper;

import com.mediocre.dto.PostHaloDTO;
import com.mediocre.entity.PostHalo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostHaloMapper {

    @Select("select id, create_time, deleted, update_time, create_from, disallow_comment, edit_time, format_content, likes, original_content, " +
            "password, status, summary, template, thumbnail, title, top_priority, url, visits from posts where status in (0, 1, 3)")
    List<PostHalo> findAll();

    @Select("select id, create_time, deleted, update_time, create_from, disallow_comment, edit_time, format_content, likes, original_content, " +
            "password, status, summary, template, thumbnail, title, top_priority, url, visits from posts where status in (0, 3)")
    List<PostHalo> findAllWithoutDraft();

    @Select("select a.*, c.name as categoryName from posts a " +
            "left join post_categories b on b.post_id = a.id " +
            "left join categories c on c.id = b.category_id " +
            "where a.status in (0,1,3) limit 5")
    List<PostHaloDTO> findAllWithCategory();

    @Select("select a.id, a.create_time, a.update_time, a.disallow_comment, a.likes, a.password," +
            "    a.status, a.summary, a.title, a.visits, c.id, c.name, c.slug_name, c.description " +
            "from posts a " +
            "left join post_categories b on b.post_id = a.id " +
            "left join categories c on c.id = b.category_id " +
            "where a.status in (0,1,3)")
    List<PostHaloDTO> findWithCategory();

    @Select("select id, create_time, deleted, update_time, create_from, disallow_comment, edit_time, format_content, likes, original_content, " +
            "password, status, summary, template, thumbnail, title, top_priority, url, visits from posts where status in (0, 1, 3) and id = #{id}")
    PostHalo findById(@Param("id") Long id);

    /**
     * 查看未被逻辑删除的post数量
     * @return 未被逻辑删除的post数量
     */
    @Select("select count(*) from posts where status in (0, 1, 3)")
    Integer count();

    /**
     * 创建一篇post
     * @param postHalo
     * @return 创建的数量
     */
    @Insert("insert into posts(id, create_time, deleted, update_time, create_from, disallow_comment, edit_time, format_content, likes, original_content," +
            "password, status, summary, template, thumbnail, title, top_priority, url, visits) values(#{id}, #{createTime}, #{deleted}, #{updateTime}, " +
            "#{createFrom}, #{disallowComment}, #{editTime}, #{formatContent}, #{likes}, #{originalContent},#{password}, #{status}, #{summary}, " +
            "#{template}, #{thumbnail}, #{title}, #{topPriority}, #{url}, #{visits})")
    int insertByPost(PostHalo postHalo);

    /**
     * 逻辑删除post
     * @param id
     * @return 删除的数量
     */
    @Update("update posts set status = 2 where id = #{id}")
    int deleteLogicallyById(Long id);

    /**
     * 物理删除post
     * @param id
     * @return 删除的数量
     */
    @Delete("delete from posts where id = #{id}")
    int deletePhysicallyById(Long id);

    /**
     * 将post还原到已发布状态
     * @param id
     * @return
     */
    @Update("update posts set status = 0 where id = #{id}")
    int restoreById(Long id);



}
